package com.tanhua.dubbo.api;

import cn.hutool.core.util.RandomUtil;
import com.tanhua.model.mongo.Option;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desciption:test
 * @Create by:金木yan
 * @Date: 2021/12/11 17:34
 */
@DubboService
public class OptionsApiImpl implements OptionsApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //根据试题id查询试题选项集合
    @Override
    public Map<ObjectId, List<Option>> findByQuestionId(ObjectId questionId) {
        //1.查询
        Query query = Query.query(Criteria.where("questionId").is(questionId));
        List<Option> options = mongoTemplate.find(query, Option.class);
        //2.构建map
        Map<ObjectId, List<Option>> map = new HashMap<>();
        map.put(questionId, options);
        return map;
    }

    //根据选项id查询选项得分
    @Override
    public Integer findById(String id) {
        Option option = mongoTemplate.findById(new ObjectId(id), Option.class);
        if (option == null){
            return RandomUtil.randomInt(5,10);
        }
        return option.getScore();
    }
}
